﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.Resources.Utility;
using csla4Skeleton.UI.SL.DanhMuc.DM_NhaCungCap;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_PhuongThucThanhToan;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_KeKhai;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiMuaSanPham;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiTienTe;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiHoaDon;
using csla4Skeleton.UI.SL.DanhMuc.DM_TaiKhoan;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_BoPhan;
using csla4Skeleton.UI.SL.DanhMuc.DM_ThueSuat;
using csla4Skeleton.UI.SL.DanhMuc.DM_TinhChatHangHoa;

using Accounting.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;
using ConfigurationNghiepVu;
namespace csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_MuaTaiSan
{
    public partial class TSCD_MuaTaiSan_MainWindow: UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    Khoa = null;
                else
                    Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = null;
            }
        }

        public string Title
        {
            get { return "MuaTaiSan"; }
        }

       
        #endregion

        #region Load Methods
        private System.Nullable<int> Khoa = null;
      
        public TSCD_MuaTaiSan_MainWindow(int Khoa)          
        {
            this.Khoa = Khoa;
        }
        public delegate void KeyUp_EventHandler(object sender, KeyEventArgs e);

        public TSCD_MuaTaiSan_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_MuaTaiSan.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            
            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.TSCD_MuaTaiSan_GeneralInformation_GroupBox, controls);
            ControlBehavior.ControlBehaviors.Navigation(this.TSCD_MuaTaiSan_MoreInformation_GroupBox, controls);
            
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                
                System.Windows.Data.CollectionViewSource TSCD_MuaTaiSan_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_MuaTaiSan_ViewModelSource"];
                if (Khoa == null)
                {
                    TSCD_MuaTaiSan_ViewModel TSCD_MuaTaiSan_ViewModel = new TSCD_MuaTaiSan_ViewModel();
                    TSCD_MuaTaiSan_ViewModel.ChildList_AddedNew += new TSCD_MuaTaiSan_ViewModel.ChildList_AddedNew_EventHandler(TSCD_MuaTaiSan_ViewModel_ChildList_AddedNew);
                    TSCD_MuaTaiSan_ViewModel.stateChanged += new TSCD_MuaTaiSan_ViewModel.DKNV_EventHandler(TSCD_MuaTaiSan_StateChanged);
                    TSCD_MuaTaiSan_ViewModel.TCHangHoaChanged += new EventHandler(TSCD_MuaTaiSan_TinhChatHangHoaChanged);
                    TSCD_MuaTaiSan_ViewModel.TSCDIdChanged += new TSCD_MuaTaiSan_ViewModel.TSCDIdChanged_EventHandler(TSCD_MuaTaiSan_TSCDIdChanged);
                    TSCD_MuaTaiSan_ViewModel.TKCoIdChanged += new TSCD_MuaTaiSan_ViewModel.TKCoIdChanged_EventHandler(TSCD_MuaTaiSan_TKCoChanged);
                   
                    TSCD_MuaTaiSan_ViewModel.LoaiTienTeChanged += new EventHandler(TSCD_MuaTaiSan_LoaiTienTeChanged);

                    TSCD_MuaTaiSan_ViewModel.ThueSuatIdChanged += new TSCD_MuaTaiSan_ViewModel.ThueSuatIdChanged_EventHandler(TSCD_MuaTaiSan_ThueSuatIdChanged);

                    TSCD_MuaTaiSan_CollectionViewSource.Source = new List<object> { TSCD_MuaTaiSan_ViewModel };

                    
                }
                else
                {
                    TSCD_MuaTaiSan_ViewModel TSCD_MuaTaiSan_ViewModel = new TSCD_MuaTaiSan_ViewModel((int)Khoa);
                    TSCD_MuaTaiSan_ViewModel.ChildList_AddedNew += new TSCD_MuaTaiSan_ViewModel.ChildList_AddedNew_EventHandler(TSCD_MuaTaiSan_ViewModel_ChildList_AddedNew);
                    TSCD_MuaTaiSan_ViewModel.stateChanged += new TSCD_MuaTaiSan_ViewModel.DKNV_EventHandler(TSCD_MuaTaiSan_StateChanged);
                    TSCD_MuaTaiSan_ViewModel.TCHangHoaChanged += new EventHandler(TSCD_MuaTaiSan_TinhChatHangHoaChanged);
                    TSCD_MuaTaiSan_ViewModel.TSCDIdChanged += new TSCD_MuaTaiSan_ViewModel.TSCDIdChanged_EventHandler(TSCD_MuaTaiSan_TSCDIdChanged);
                    TSCD_MuaTaiSan_ViewModel.TKCoIdChanged += new TSCD_MuaTaiSan_ViewModel.TKCoIdChanged_EventHandler(TSCD_MuaTaiSan_TKCoChanged);
                    TSCD_MuaTaiSan_ViewModel.LoaiTienTeChanged += new EventHandler(TSCD_MuaTaiSan_LoaiTienTeChanged);
                    
                    TSCD_MuaTaiSan_ViewModel.ThueSuatIdChanged += new TSCD_MuaTaiSan_ViewModel.ThueSuatIdChanged_EventHandler(TSCD_MuaTaiSan_ThueSuatIdChanged);

                    TSCD_MuaTaiSan_CollectionViewSource.Source = new List<object> { TSCD_MuaTaiSan_ViewModel };

                    
                }

                System.Windows.Data.CollectionViewSource dm_NhaCungCap_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
                DM_NhaCungCap_ListViewModel dm_NhaCungCap_ListViewModel = new DM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_NhaCungCap);
                dm_NhaCungCap_CollectionViewSource.Source = new List<object> { dm_NhaCungCap_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_PhuongThucThanhToan_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_PhuongThucThanhToan_ListViewModelSource"];
                DM_PhuongThucThanhToan_ListViewModel dm_PhuongThucThanhToan_ListViewModel = new DM_PhuongThucThanhToan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_PhuongThucThanhToan_CollectionViewSource.Source = new List<object> { dm_PhuongThucThanhToan_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_BoPhan_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_BoPhan_ListViewModelSource"];
                DM_BoPhan_ListViewModel dm_BoPhan_ListViewModel = new DM_BoPhan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_BoPhan_CollectionViewSource.Source = new List<object> { dm_BoPhan_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiMuaSanPham_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiMuaSanPham_ListViewModelSource"];
                DM_LoaiMuaSanPham_ListViewModel dm_LoaiMuaSanPham_ListViewModel = new DM_LoaiMuaSanPham_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiMuaSanPham_CollectionViewSource.Source = new List<object> { dm_LoaiMuaSanPham_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiTienTe_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
                DM_LoaiTienTe_ListViewModel dm_LoaiTienTe_ListViewModel = new DM_LoaiTienTe_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiTienTe_CollectionViewSource.Source = new List<object> { dm_LoaiTienTe_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiHoaDon_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiHoaDon_ListViewModelSource"];
                DM_LoaiHoaDon_ListViewModel dm_LoaiHoaDon_ListViewModel = new DM_LoaiHoaDon_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiHoaDon_CollectionViewSource.Source = new List<object> { dm_LoaiHoaDon_ListViewModel };

                System.Windows.Data.CollectionViewSource tscd_KeKhai_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_KeKhai_ListViewModelSource"];
                TSCD_KeKhai_ListViewModel tscd_KeKhai_ListViewModel = new TSCD_KeKhai_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                tscd_KeKhai_CollectionViewSource.Source = new List<object> { tscd_KeKhai_ListViewModel };

               
                System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
                DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = new DM_ThueSuat_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_ThueSuat_CollectionViewSource.Source = new List<object> { dm_ThueSuat_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TinhChatHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TinhChatHangHoa_ListViewModelSource"];
                DM_TinhChatHangHoa_ListViewModel dm_TinhChatHangHoa_ListViewModel = new DM_TinhChatHangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TinhChatHangHoa_CollectionViewSource.Source = new List<object> { dm_TinhChatHangHoa_ListViewModel };

                DoFilterOnDefaultDKNV1();

               
                DoFilterOnDefaultDM_SanPham();

                this.TSCD_MuaTaiSan_MuaTaiSanChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.TSCD_MuaTaiSan_MuaTaiSanChiTietGridView);
                this.TSCD_MuaTaiSan_ThueChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.TSCD_MuaTaiSan_ThueChiTietGridView);
                this.TSCD_MuaTaiSan_ChiPhiMuaGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.TSCD_MuaTaiSan_ChiPhiMuaGridView);
                
                this.TSCD_MuaTaiSan_ThueNKGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.TSCD_MuaTaiSan_ThueNKGridView);
                this.TSCD_MuaTaiSan_ThueTTDBGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.TSCD_MuaTaiSan_ThueTTDBGridView);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TSCD_MuaTaiSan_MuaTaiSanChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TSCD_MuaTaiSan_ThueChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TSCD_MuaTaiSan_ChiPhiMuaGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TSCD_MuaTaiSan_ThueNKGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TSCD_MuaTaiSan_ThueTTDBGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TSCD_MuaTaiSan_MuaTaiSanChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TSCD_MuaTaiSan_ThueChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TSCD_MuaTaiSan_ChiPhiMuaGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TSCD_MuaTaiSan_ThueNKGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TSCD_MuaTaiSan_ThueTTDBGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
               
            }
        }

        #endregion
       

        #region Event Handle
        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                TSCD_MuaTaiSan_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave)
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }
                else if (e.Key == Key.F6)
                {
                    AddItems();
                }
                else if (e.Key == Key.D1)
                {
                    this.TSCD_MuaTaiSan_MuaTaiSanChiTietTab.IsSelected = true;
                }
                else if (e.Key == Key.D2)
                {
                    this.TSCD_MuaTaiSan_ThueChiTietTab.IsSelected = true;
                }
                else if (e.Key == Key.D3)
                {
                    this.TSCD_MuaTaiSan_ChiPhiMuaTab.IsSelected = true;
                }
                
                else if (e.Key == Key.D4)
                {
                    this.TSCD_MuaTaiSan_ThueNKTab.IsSelected = true;
                }
                else if (e.Key == Key.D5)
                {
                    this.TSCD_MuaTaiSan_ThueTTDBTab.IsSelected = true;
                }
            }
            e.Handled = true;
        }

        void AddItemButton_Click(object sender, RoutedEventArgs e)
        {

            AddItems();
        }

        void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteItems();
          
        }

        public void AddItems()
        {
            if (this.TSCD_MuaTaiSan_MuaTaiSanChiTietTab.IsSelected)
            {
                AddMuaTaiSanDetailsItem();
            }
        }
        private RadGridView GetActiveGridView()
        {
            if (this.TSCD_MuaTaiSan_MuaTaiSanChiTietTab.IsSelected)
            {
                return TSCD_MuaTaiSan_MuaTaiSanChiTietGridView;
            }
            else if (this.TSCD_MuaTaiSan_ThueChiTietTab.IsSelected)
            {
                return TSCD_MuaTaiSan_ThueChiTietGridView;
            }
            else if (this.TSCD_MuaTaiSan_ChiPhiMuaTab.IsSelected)
            {
                return TSCD_MuaTaiSan_ChiPhiMuaGridView;
            }
           
            else if (this.TSCD_MuaTaiSan_ThueNKTab.IsSelected)
            {
                return TSCD_MuaTaiSan_ThueNKGridView;
            }
            else if (this.TSCD_MuaTaiSan_ThueTTDBTab.IsSelected)
            {
                return TSCD_MuaTaiSan_ThueTTDBGridView;
            }
            return null;
        }

        void DeleteItems()
        {
            if (this.TSCD_MuaTaiSan_MuaTaiSanChiTietTab.IsSelected)
            {
                TSCD_MuaTaiSan_ChiTietChild ChiTietChild = (TSCD_MuaTaiSan_ChiTietChild)(this.TSCD_MuaTaiSan_MuaTaiSanChiTietGridView.SelectedItem);
                IList<TSCD_MuaTaiSan_ChiTietChild> list = GetViewModel().Model.TSCD_MuaTaiSan_ChiTiets;
                int index = list.IndexOf(ChiTietChild);
                
                DeleteMuaTaiSanDetailsItem(index);
            }
          

        }
        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            if (grid.Items.Count > 0)
            {
                GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                    grid.CurrentCell.BeginEdit();
                }));
            }
        }
              
        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
           TSCD_MuaTaiSan_ViewModel vm = GetViewModel();
           vm.SaveAndNew();
        }

       
        
        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddMuaTaiSanDetailsItem();
                }
            }
            else
            {
                ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, controls);
            }
        }

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddMuaTaiSanDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
            }
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddMuaTaiSanDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
            }
        }
       

        private void BookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().GhiSo();
        }

        private void DeleteBookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().BoGhi();
        }

        void OnTabKeyPress(object sender, EventArgs e)
        {
            this.tabControl.SelectedItem = tabControl.Items[0];
            this.AddMuaTaiSanDetailsItem();
        }
      
        void OnReachLastColumn(object sender, ReachLastColumnArgs e)
        {

            List<TabItem> AvailableTabItem = new List<TabItem>();
            int indexCurrentTab = 0;
            foreach (TabItem item in this.tabControl.Items)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    AvailableTabItem.Add(item);
                    if (item.GetHashCode() == tabControl.SelectedItem.GetHashCode())
                    {
                        indexCurrentTab = AvailableTabItem.IndexOf(item);
                    }

                }
            }


            TabItem[] list = AvailableTabItem.ToArray();
            if (indexCurrentTab == (AvailableTabItem.Count - 1))
            {
                this.tabControl.SelectedItem = list[0];
                FocusAfterChangeTab(this.TSCD_MuaTaiSan_MuaTaiSanChiTietGridView, e.Index);

            }
            else
            {
                this.tabControl.SelectedItem = list[indexCurrentTab + 1];
                if (list[indexCurrentTab + 1].Name.Equals("TSCD_MuaTaiSan_ChiPhiMuaTab"))
                {
                    FocusAfterChangeTab(this.TSCD_MuaTaiSan_ChiPhiMuaGridView, e.Index);
                }
                else if (list[indexCurrentTab + 1].Name.Equals("TSCD_MuaTaiSan_ThueChiTietTab"))
                {
                    FocusAfterChangeTab(this.TSCD_MuaTaiSan_ThueChiTietGridView, e.Index);

                }
                else if (list[indexCurrentTab + 1].Name.Equals("TSCD_MuaTaiSan_ThueNKTab"))
                {
                    FocusAfterChangeTab(this.TSCD_MuaTaiSan_ThueNKGridView, e.Index);

                }
                else if (list[indexCurrentTab + 1].Name.Equals("TSCD_MuaTaiSan_ThueTTDBTab"))
                {
                    FocusAfterChangeTab(this.TSCD_MuaTaiSan_ThueTTDBGridView, e.Index);

                }
               
            }

        }

        private void FocusAfterChangeTab(GridViewDataControl grid, int index)
        {
          
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[index], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
           
        }
        private void TSCD_MuaTaiSan_GeneralInformation_NhaCungCapId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_NhaCungCap_ChildWindow dm_NhaCungCap_ChildWindow = new DM_NhaCungCap_ChildWindow();
            dm_NhaCungCap_ChildWindow.Show();
            dm_NhaCungCap_ChildWindow.SaveClicked += new EventHandler(dm_NhaCungCap_ViewModel_Saved);
        }

        private void TSCD_MuaTaiSan_MoreInformation_PhuongThucThanhToanId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_PhuongThucThanhToan_ChildWindow dm_PhuongThucThanhToan_ChildWindow = new DM_PhuongThucThanhToan_ChildWindow();
            dm_PhuongThucThanhToan_ChildWindow.Show();
            dm_PhuongThucThanhToan_ChildWindow.SaveClicked += new EventHandler(dm_PhuongThucThanhToan_ViewModel_Saved);
        }

        private void TSCD_MuaTaiSan_MoreInformation_LoaiTienId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_LoaiTienTe_ChildWindow dm_LoaiTienTe_ChildWindow = new DM_LoaiTienTe_ChildWindow();
            dm_LoaiTienTe_ChildWindow.Show();
            dm_LoaiTienTe_ChildWindow.SaveClicked += new EventHandler(dm_LoaiTienTe_ViewModel_Saved);
        }

        private void TSCD_MuaTaiSan_MoreInformation_LoaiMuaSanPhamId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_LoaiMuaSanPham_ChildWindow dm_LoaiMuaSanPham_ChildWindow = new DM_LoaiMuaSanPham_ChildWindow();
            dm_LoaiMuaSanPham_ChildWindow.Show();
            dm_LoaiMuaSanPham_ChildWindow.SaveClicked += new EventHandler(dm_LoaiMuaSanPham_ViewModel_Saved);
        }
       
        #endregion

        #region View Model Related Methods
        private TSCD_MuaTaiSan_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource TSCD_MuaTaiSan_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_MuaTaiSan_ViewModelSource"];
            return ((List<object>)TSCD_MuaTaiSan_CollectionViewSource.Source)[0] as TSCD_MuaTaiSan_ViewModel;
        }

        void dm_NhaCungCap_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_NhaCungCap_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
            DM_NhaCungCap_ListViewModel vm = ((List<object>)dm_NhaCungCap_ListViewSource.Source)[0] as DM_NhaCungCap_ListViewModel;
            vm.GetDM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_NhaCungCap);
        }

        void dm_LoaiTienTe_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_LoaiTienTe_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
            DM_LoaiTienTe_ListViewModel vm = ((List<object>)dm_LoaiTienTe_ListViewSource.Source)[0] as DM_LoaiTienTe_ListViewModel;
            vm.GetDM_LoaiTienTe_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_LoaiMuaSanPham_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_LoaiMuaSanPham_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiMuaSanPham_ListViewModelSource"];
            DM_LoaiMuaSanPham_ListViewModel vm = ((List<object>)dm_LoaiMuaSanPham_ListViewSource.Source)[0] as DM_LoaiMuaSanPham_ListViewModel;
            vm.GetDM_LoaiMuaSanPham_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_PhuongThucThanhToan_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_PhuongThucThanhToan_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_PhuongThucThanhToan_ListViewModelSource"];
            DM_PhuongThucThanhToan_ListViewModel vm = ((List<object>)dm_PhuongThucThanhToan_ListViewSource.Source)[0] as DM_PhuongThucThanhToan_ListViewModel;
            vm.GetDM_PhuongThucThanhToan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        public void DoFilterOnDefaultDM_SanPham()
        {

            System.Windows.Data.CollectionViewSource TSCD_KeKhai_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_KeKhai_ListViewModelSource"];
            TSCD_KeKhai_ListViewModel tscd_KeKhai_ListViewModel = new TSCD_KeKhai_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            TSCD_KeKhai_CollectionViewSource.Source = new List<object> { tscd_KeKhai_ListViewModel };
           
        }
       
        void TSCD_MuaTaiSan_TKCoChanged(object sender, TKCoIdChangedEventArgs e)
        {
            if (this.GetViewModel().Model.LoaiMuaSanPhamId == 3)
            {
                TSCD_MuaTaiSan_ChiTietChild ChiTietChild = GetViewModel().Model.TSCD_MuaTaiSan_ChiTiets[e.Index];
                TSCD_MuaTaiSan_ThueChild ThueChild = GetViewModel().Model.TSCD_MuaTaiSan_Thues[e.Index];
                ThueChild.TKCoId = ChiTietChild.TKCoId;
                
            }
            

        }

        void TSCD_MuaTaiSan_ThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {
            
            System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
            DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = ((List<object>)dm_ThueSuat_CollectionViewSource.Source)[0] as DM_ThueSuat_ListViewModel;    
            
            TSCD_MuaTaiSan_ThueChild ThueChild = GetViewModel().Model.TSCD_MuaTaiSan_Thues[e.Index];
            IList<DM_ThueSuatInfo> list = dm_ThueSuat_ListViewModel.Model.getListOfInfo();
            foreach (DM_ThueSuatInfo item in list)
            {
                if (item.Khoa == e.ThueSuatId)
                {
                    ThueChild.ThueSuatValue = item.GiaTriTinhThue;
                }
            }

           

        }
        void TSCD_MuaTaiSan_TinhChatHangHoaChanged(object sender, EventArgs e)
        {
            
        }

        void TSCD_MuaTaiSan_LoaiTienTeChanged(object sender, EventArgs e)
        {
            
            System.Windows.Data.CollectionViewSource dm_LoaiTienTe_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
            DM_LoaiTienTe_ListViewModel dm_LoaiTienTe_ListViewModel = ((List<object>)dm_LoaiTienTe_CollectionViewSource.Source)[0] as DM_LoaiTienTe_ListViewModel;
            IList<DM_LoaiTienTeInfo> list = dm_LoaiTienTe_ListViewModel.Model.getListOfInfo();
            foreach( DM_LoaiTienTeInfo item in list)
            {
                if (item.Khoa == GetViewModel().Model.LoaiTienId)
                {
                    GetViewModel().Model.TyGia = item.TyGiaQuyDoi;
                    break;
                }
            }
        }
       
        
        void TSCD_MuaTaiSan_TSCDIdChanged(object sender, TSCDIdChangedEventArgs e)
        {
            
            TSCD_MuaTaiSan_ChiTietChild ChiTietChild =  GetViewModel().Model.TSCD_MuaTaiSan_ChiTiets[e.Index];
            TSCD_MuaTaiSan_ThueChild ThueChild =  GetViewModel().Model.TSCD_MuaTaiSan_Thues[e.Index];
            TSCD_MuaTaiSan_ChiPhiMuaChild ChiPhiMuaChild =  GetViewModel().Model.TSCD_MuaTaiSan_ChiPhiMuas[e.Index];
            
            TSCD_MuaTaiSan_ThueNKChild ThueNKChild = GetViewModel().Model.TSCD_MuaTaiSan_ThueNKs[e.Index];
            TSCD_MuaTaiSan_ThueTTDBChild ThueTTDBChild = GetViewModel().Model.TSCD_MuaTaiSan_ThueTTDBs[e.Index];

            System.Windows.Data.CollectionViewSource tscd_KeKhai_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_KeKhai_ListViewModelSource"];
            TSCD_KeKhai_ListViewModel tscd_KeKhai_ListViewModel = ((List<object>)tscd_KeKhai_CollectionViewSource.Source)[0] as TSCD_KeKhai_ListViewModel;
            System.Collections.Generic.IList<TSCD_KeKhaiInfo> list = tscd_KeKhai_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TKNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiTiet_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKNo_ChiTiet_ListViewModel = ((List<object>)dm_TKNo_ChiTiet_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_ChiTiet = dm_TKNo_ChiTiet_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TKCo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiTiet_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKCo_ChiTiet_ListViewModel = ((List<object>)dm_TKCo_ChiTiet_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKCo_ChiTiet = dm_TKCo_ChiTiet_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TKNo_ChiPhiMua_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiPhiMua_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKNo_ChiPhiMua_ListViewModel = ((List<object>)dm_TKNo_ChiPhiMua_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_ChiPhiMua = dm_TKNo_ChiPhiMua_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TKCo_ChiPhiMua_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiPhiMua_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKCo_ChiPhiMua_ListViewModel = ((List<object>)dm_TKCo_ChiPhiMua_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKCo_ChiPhiMua = dm_TKCo_ChiPhiMua_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TKNo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_Thue_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKNo_Thue_ListViewModel = ((List<object>)dm_TKNo_Thue_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_Thue = dm_TKNo_Thue_ListViewModel.Model.getListOfInfo();

           
            System.Windows.Data.CollectionViewSource dm_TKNo_ThueNK_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ThueNK_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKNo_ThueNK_ListViewModel = ((List<object>)dm_TKNo_ThueNK_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_ThueNK = dm_TKNo_ThueNK_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TKCo_ThueNK_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ThueNK_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKCo_ThueNK_ListViewModel = ((List<object>)dm_TKCo_ThueNK_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKCo_ThueNK = dm_TKCo_ThueNK_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TKNo_ThueTTDB_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ThueTTDB_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKNo_ThueTTDB_ListViewModel = ((List<object>)dm_TKNo_ThueTTDB_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_ThueTTDB = dm_TKNo_ThueTTDB_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TKCo_ThueTTDB_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ThueTTDB_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKCo_ThueTTDB_ListViewModel = ((List<object>)dm_TKCo_ThueTTDB_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKCo_ThueTTDB = dm_TKCo_ThueTTDB_ListViewModel.Model.getListOfInfo();


            foreach (TSCD_KeKhaiInfo item in list)
            {
                if (item.Khoa == e.TSCDId)
                {
                    ChiTietChild.DienGiai = item.Ten;
                    ChiTietChild.BoPhanId = item.BoPhanId;
                    ChiTietChild.TKNoId = listTKNo_ChiTiet[0].Khoa;
                    ChiTietChild.TKCoId = listTKCo_ChiTiet[0].Khoa;

                    ThueChild.TSCDId = item.Khoa;
                    ThueChild.DienGiai = item.Ten;
                    ThueChild.TKNoId = listTKNo_Thue[0].Khoa;
                    ThueChild.TKCoId = ChiTietChild.TKCoId;

                    ThueNKChild.TSCDId = item.Khoa;
                    ThueNKChild.DienGiai = item.Ten;
                    ThueNKChild.TkNoId = listTKNo_ThueNK[0].Khoa;
                    ThueNKChild.TkCoId = listTKCo_ThueNK[0].Khoa;
                   
                    ChiPhiMuaChild.TSCDId = item.Khoa;
                    ChiPhiMuaChild.DienGiai = item.Ten;
                    ChiPhiMuaChild.TkNoId = listTKNo_ChiPhiMua[0].Khoa;
                    ChiPhiMuaChild.TkCoId = listTKCo_ChiPhiMua[0].Khoa;

                    ThueTTDBChild.TSCDId = item.Khoa;
                    ThueTTDBChild.DienGiai = item.Ten;
                    ThueTTDBChild.TkNoId = listTKNo_ThueTTDB[0].Khoa;
                    ThueTTDBChild.TkCoId = listTKCo_ThueTTDB[0].Khoa;
                    break;
                }
            }

         

        }

        void TSCD_MuaTaiSan_StateChanged(object sender, DieuKienNghiepVuEventArgs e)
        {
           
            
        }

        public void DoFilterOnDefaultDKNV1()
        {

            System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiTiet_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanNo_ChiTiet_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanNoChiTietChanged);
            dm_TaiKhoanNo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_ChiTiet_ListViewModel };

            System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiTiet_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanCo_ChiTiet_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanCoChiTietChanged);
            dm_TaiKhoanCo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_ChiTiet_ListViewModel };

            System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_Thue_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_Thue_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanNo_Thue_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanNoThueChanged);
            dm_TaiKhoanNo_Thue_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_Thue_ListViewModel };

            System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_Thue_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_Thue_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanCo_Thue_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanCoThueChanged);
            dm_TaiKhoanCo_Thue_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_Thue_ListViewModel };

            System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_ChiPhiMua_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiPhiMua_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_ChiPhiMua_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanNo_ChiPhiMua_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanNoChiPhiMuaChanged);
            dm_TaiKhoanNo_ChiPhiMua_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_ChiPhiMua_ListViewModel };

            System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_ChiPhiMua_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiPhiMua_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_ChiPhiMua_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanCo_ChiPhiMua_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanCoChiPhiMuaChanged);
            dm_TaiKhoanCo_ChiPhiMua_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_ChiPhiMua_ListViewModel };

            System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_ThueNK_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ThueNK_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_ThueNK_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanNo_ThueNK_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanNoThueNKChanged);
            dm_TaiKhoanNo_ThueNK_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_ThueNK_ListViewModel };

            System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_ThueNK_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ThueNK_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_ThueNK_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanCo_ThueNK_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanCoThueNKChanged);
            dm_TaiKhoanCo_ThueNK_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_ThueNK_ListViewModel };

            System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_ThueTTDB_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ThueTTDB_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_ThueTTDB_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanNo_ThueTTDB_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanNoThueTTDBChanged);
            dm_TaiKhoanNo_ThueTTDB_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_ThueTTDB_ListViewModel };

            System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_ThueTTDB_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ThueTTDB_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_ThueTTDB_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_TaiKhoanCo_ThueTTDB_ListViewModel.ListTaiKhoanChanged += new EventHandler(OnTaiKhoanCoThueTTDBChanged);
            dm_TaiKhoanCo_ThueTTDB_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_ThueTTDB_ListViewModel };
        
        
        }
       

        private void OnTaiKhoanNoChiTietChanged(object sender, EventArgs e)
        {
            
            System.Windows.Data.CollectionViewSource dm_TKNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiTiet_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKNo_ChiTiet_ListViewModel = ((List<object>)dm_TKNo_ChiTiet_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            if( dm_TKNo_ChiTiet_ListViewModel.Model != null && GetViewModel().Model != null)
            {
                System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_ChiTiet = dm_TKNo_ChiTiet_ListViewModel.Model.getListOfInfo();
            
                IList<TSCD_MuaTaiSan_ChiTietChild> list = this.GetViewModel().Model.TSCD_MuaTaiSan_ChiTiets;
                foreach (TSCD_MuaTaiSan_ChiTietChild item in list)
                {
                    item.TKNoId = listTKNo_ChiTiet[0].Khoa;
                }
            }
        }
            

        private void OnTaiKhoanNoThueChanged(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TKNo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_Thue_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKNo_Thue_ListViewModel = ((List<object>)dm_TKNo_Thue_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            if (dm_TKNo_Thue_ListViewModel.Model != null && GetViewModel().Model != null)
            {
                System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_Thue = dm_TKNo_Thue_ListViewModel.Model.getListOfInfo();
            
                IList<TSCD_MuaTaiSan_ThueChild> list = this.GetViewModel().Model.TSCD_MuaTaiSan_Thues;
                foreach (TSCD_MuaTaiSan_ThueChild item in list)
                {
                    item.TKNoId = listTKNo_Thue[0].Khoa;
                }
            }
        }

        private void OnTaiKhoanNoChiPhiMuaChanged(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TKNo_ChiPhiMua_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiPhiMua_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKNo_ChiPhiMua_ListViewModel = ((List<object>)dm_TKNo_ChiPhiMua_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            if (dm_TKNo_ChiPhiMua_ListViewModel.Model != null && GetViewModel().Model != null)
            {
                System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_ChiPhiMua = dm_TKNo_ChiPhiMua_ListViewModel.Model.getListOfInfo();

                IList<TSCD_MuaTaiSan_ChiPhiMuaChild> list = this.GetViewModel().Model.TSCD_MuaTaiSan_ChiPhiMuas;
                foreach (TSCD_MuaTaiSan_ChiPhiMuaChild item in list)
                {
                    item.TkNoId = listTKNo_ChiPhiMua[0].Khoa;
                }
            }
        }

        private void OnTaiKhoanNoThueNKChanged(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TKNo_ThueNK_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ThueNK_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKNo_ThueNK_ListViewModel = ((List<object>)dm_TKNo_ThueNK_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            if (dm_TKNo_ThueNK_ListViewModel.Model != null && GetViewModel().Model != null)
            {
                System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_ThueNK = dm_TKNo_ThueNK_ListViewModel.Model.getListOfInfo();

                IList<TSCD_MuaTaiSan_ThueNKChild> list = this.GetViewModel().Model.TSCD_MuaTaiSan_ThueNKs;
                foreach (TSCD_MuaTaiSan_ThueNKChild item in list)
                {
                    item.TkNoId = listTKNo_ThueNK[0].Khoa;
                }
            }
        }

        private void OnTaiKhoanNoThueTTDBChanged(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TKNo_ThueTTDB_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ThueTTDB_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKNo_ThueTTDB_ListViewModel = ((List<object>)dm_TKNo_ThueTTDB_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            if (dm_TKNo_ThueTTDB_ListViewModel.Model != null && GetViewModel().Model != null)
            {
                System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKNo_ThueTTDB = dm_TKNo_ThueTTDB_ListViewModel.Model.getListOfInfo();

                IList<TSCD_MuaTaiSan_ThueTTDBChild> list = this.GetViewModel().Model.TSCD_MuaTaiSan_ThueTTDBs;
                foreach (TSCD_MuaTaiSan_ThueTTDBChild item in list)
                {
                    item.TkNoId = listTKNo_ThueTTDB[0].Khoa;
                }
            }
        }


        private void OnTaiKhoanCoChiTietChanged(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TKCo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiTiet_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKCo_ChiTiet_ListViewModel = ((List<object>)dm_TKCo_ChiTiet_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            if (dm_TKCo_ChiTiet_ListViewModel.Model != null && GetViewModel().Model != null)
            {
                System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKCo_ChiTiet = dm_TKCo_ChiTiet_ListViewModel.Model.getListOfInfo();

                IList<TSCD_MuaTaiSan_ChiTietChild> list = this.GetViewModel().Model.TSCD_MuaTaiSan_ChiTiets;
                foreach (TSCD_MuaTaiSan_ChiTietChild item in list)
                {
                    item.TKCoId = listTKCo_ChiTiet[0].Khoa;
                }
            }
        }

        private void OnTaiKhoanCoThueChanged(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TKCo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_Thue_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKCo_Thue_ListViewModel = ((List<object>)dm_TKCo_Thue_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            if (dm_TKCo_Thue_ListViewModel.Model != null && GetViewModel().Model != null)
            {
                System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKCo_Thue = dm_TKCo_Thue_ListViewModel.Model.getListOfInfo();

                IList<TSCD_MuaTaiSan_ThueChild> list = this.GetViewModel().Model.TSCD_MuaTaiSan_Thues;
                foreach (TSCD_MuaTaiSan_ThueChild item in list)
                {
                    item.TKCoId = listTKCo_Thue[0].Khoa;
                }
            }
        }

        private void OnTaiKhoanCoChiPhiMuaChanged(object sender, EventArgs e)
        {

            System.Windows.Data.CollectionViewSource dm_TKCo_ChiPhiMua_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiPhiMua_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKCo_ChiPhiMua_ListViewModel = ((List<object>)dm_TKCo_ChiPhiMua_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            if (dm_TKCo_ChiPhiMua_ListViewModel.Model != null && GetViewModel().Model != null)
            {
                System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKCo_ChiPhiMua = dm_TKCo_ChiPhiMua_ListViewModel.Model.getListOfInfo();

                IList<TSCD_MuaTaiSan_ChiPhiMuaChild> list = this.GetViewModel().Model.TSCD_MuaTaiSan_ChiPhiMuas;
                foreach (TSCD_MuaTaiSan_ChiPhiMuaChild item in list)
                {
                    item.TkCoId = listTKCo_ChiPhiMua[0].Khoa;
                }
            }
        }

        private void OnTaiKhoanCoThueNKChanged(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TKCo_ThueNK_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ThueNK_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKCo_ThueNK_ListViewModel = ((List<object>)dm_TKCo_ThueNK_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            if (dm_TKCo_ThueNK_ListViewModel.Model != null && GetViewModel().Model != null)
            {
                System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKCo_ThueNK = dm_TKCo_ThueNK_ListViewModel.Model.getListOfInfo();

                IList<TSCD_MuaTaiSan_ThueNKChild> list = this.GetViewModel().Model.TSCD_MuaTaiSan_ThueNKs;
                foreach (TSCD_MuaTaiSan_ThueNKChild item in list)
                {
                    item.TkCoId = listTKCo_ThueNK[0].Khoa;
                }
            }
        }

        private void OnTaiKhoanCoThueTTDBChanged(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TKCo_ThueTTDB_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ThueTTDB_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TKCo_ThueTTDB_ListViewModel = ((List<object>)dm_TKCo_ThueTTDB_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            if (dm_TKCo_ThueTTDB_ListViewModel.Model != null && GetViewModel().Model != null)
            {
                System.Collections.Generic.IList<DM_TaiKhoanInfo> listTKCo_ThueTTDB = dm_TKCo_ThueTTDB_ListViewModel.Model.getListOfInfo();

                IList<TSCD_MuaTaiSan_ThueTTDBChild> list = this.GetViewModel().Model.TSCD_MuaTaiSan_ThueTTDBs;
                foreach (TSCD_MuaTaiSan_ThueTTDBChild item in list)
                {
                    item.TkCoId = listTKCo_ThueTTDB[0].Khoa;
                }
            }
           
        }

        
        private void AddThueDetailsItem()
        {
            GetViewModel().AddTSCD_MuaTaiSan_ThueItem();
        }

        private void AddMuaTaiSanDetailsItem()
        {
            GetViewModel().AddTSCD_MuaTaiSan_ChiTietItem();
        }

        private void AddChiPhiMuaDetailsItem()
        {
            GetViewModel().AddTSCD_MuaTaiSan_ChiPhiMuaItem();
        }

        private void AddThueNKDetailsItem()
        {
            GetViewModel().AddTSCD_MuaTaiSan_ThueNKItem();
        }

        private void AddThueTTDBDetailsItem()
        {
            GetViewModel().AddTSCD_MuaTaiSan_ThueTTDBItem();
        }

        private void DeleteThueDetailsItem(int Index)
        {
            GetViewModel().DeleteTSCD_MuaTaiSan_ThueItem(Index);
        }

        private void DeleteMuaTaiSanDetailsItem(int Index)
        {
            GetViewModel().DeleteTSCD_MuaTaiSan_ChiTietItem( Index);
        }

        private void DeleteChiPhiMuaDetailsItem(int Index)
        {
            GetViewModel().DeleteTSCD_MuaTaiSan_ChiPhiMuaItem(Index);
        }

       
        private void DeleteThueNKDetailsItem(int Index)
        {
            GetViewModel().DeleteTSCD_MuaTaiSan_ThueNKItem(Index);
        }

        private void DeleteThueTTDBDetailsItem(int Index)
        {
            GetViewModel().DeleteTSCD_MuaTaiSan_ThueTTDBItem(Index);
        }

        void TSCD_MuaTaiSan_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "TSCD_MuaTaiSan_ChiTiets")
            {
                this.TSCD_MuaTaiSan_MuaTaiSanChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "TSCD_MuaTaiSan_Thues")
            {
                this.TSCD_MuaTaiSan_ChiPhiMuaGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "TSCD_MuaTaiSan_ChiPhiMuas")
            {
                this.TSCD_MuaTaiSan_ChiPhiMuaGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
           
            else if (e.ChildListName == "TSCD_MuaTaiSan_ThueNKs")
            {
                this.TSCD_MuaTaiSan_ThueNKGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "TSCD_MuaTaiSan_ThueTTDBs")
            {
                this.TSCD_MuaTaiSan_ThueTTDBGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
        }

       
        #endregion

       
    }
}
